package ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import midlet.VadeMecumMIDlet;
import multimedia.SaveSnapshotThread;
import multimedia.SendToServiceWithContextThread;
import multimedia.SendToServiceWithoutContextThread;

public class CreateNoteForm extends Form implements CommandListener {
	
	private VadeMecumMIDlet midlet;
	
	private Displayable previous;
	
	private Command save = null;
	private Command sendToServiceWhithoutContext = null;
	private Command sendToServiceWithContext = null;
	private Command back = null;
	
	private TextField note = null;
	
	public CreateNoteForm (VadeMecumMIDlet midlet, Displayable previous) {
		super("Create a text note");
		
		this.midlet = midlet;
		this.previous = previous;
		
		save = new Command("Save note", Command.OK, 1);
		sendToServiceWhithoutContext = new Command("Send without context", Command.OK, 1);
		sendToServiceWithContext = new Command("Send with context", Command.OK, 1);
		back = new Command("BACK", Command.BACK, 1);
		addCommand(save);
		addCommand(sendToServiceWhithoutContext);
		addCommand(sendToServiceWithContext);
		addCommand(back);
		
		note = new TextField("", "", 500, TextField.ANY);
		append(note);
		setCommandListener(this);
	}

	public void commandAction(Command cmd, Displayable disp) {
		if (this.save == cmd) {
			saveSnapshot();
		} else if (this.sendToServiceWhithoutContext == cmd) {
			sendWithoutContext();
		} else if (this.sendToServiceWithContext == cmd) {
			sendWithContext();
		} else if(this.back == cmd) {
			midlet.display.setCurrent(this.previous);
		}		
	}
	
	public void saveSnapshot() {
		Thread saveThread = new Thread(new SaveSnapshotThread(midlet, this, note.getString().getBytes(), System.currentTimeMillis()));
		saveThread.start();
	}

	public void sendWithoutContext() {
		Thread sendThread = new Thread(new SendToServiceWithoutContextThread(midlet, this, note.getString().getBytes(), System.currentTimeMillis()));
		sendThread.start();
	}

	public void sendWithContext() {
		Thread sendThread = new Thread(new SendToServiceWithContextThread(midlet, this, note.getString().getBytes(), System.currentTimeMillis()));
		sendThread.start();
	}

}
